71E - Nuclear Fusion - CodeForces Solution


bitmasks dp *2200

Please click on ads to support us..

C++ Code:

// LUOGU_RID: 115684560
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int M = 20;
string s[]={" ","H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm"};
int n,m,a[M],b[M],f[M][1<<18];
string t;vector<int> v;
int work(string x){
	for(int i=1;i<=100;i++)
		if(s[i]==x) return i;
}
void print(){
	int sum=0,j=0;
	for(int i=0;i<v.size();i++){
		sum+=v[i];
		cout<<s[v[i]];
		if(sum==b[j]){
			printf("->");
			cout<<s[b[j++]];
			puts("");sum=0;
		}
		else printf("+");
	}
	exit(0);
}
void dfs(int x,int sum,int s){
	if(sum==b[x]){
		if(x==m){
			puts("YES");
			print();
		}
		else dfs(x+1,0,s);
		return ;
	}
	if(sum>b[x]) return ;
	if(f[x][s]) return ;
	for(int i=0;i<n;i++){
		if(s&(1<<i)) continue;
		v.push_back(a[i]);
		dfs(x,sum+a[i],s|(1<<i));
		v.pop_back();
	}
	f[x][s]=1;
}
signed main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>t;
		a[i]=work(t);
	}
	for(int i=0;i<m;i++){
		cin>>t;
		b[i]=work(t);
	}
	dfs(0,0,0);cout<<"NO";
	return 0;
}


Comments

Submit
0 Comments
More Questions

1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff